﻿using CommonTools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace shop.Web.Controllers.api
{
    [Route("api/[controller]")]
    [ApiController]
    public class saveTradeListController : ControllerBase
    {

        IFreeSql _sql;

        public saveTradeListController(IFreeSql sql)
        {
            _sql = sql;
        }


        [HttpPost]
        //public async Task<Result> Index([FromBody]string data)
        public async Task<Result> Index([FromForm]string data)
        {
            if(string.IsNullOrEmpty(data))
            {
                return new Result(true);
            }

            var ind = data.IndexOf("erp订单表的主键");

            if (ind <0 )
            {

                return new Result(false, "传入的参数无效");
            }

            var headerEnd = data.IndexOf(']', ind);
            if (headerEnd < 0)
            {
                return new Result(false, "传入的参数无效");
            }

            //所有的列
            var headers = data.Substring(ind, headerEnd - ind)
                .Split(',')
                .Select(a => a.Trim().Trim('\''))
                .ToArray();

            //共计125个列 
            // ['erp订单表的主键', '订单编号', '平台ID', '店铺平台ID', '店铺编号', '店铺名称', '店铺备注', '仓库类型', '仓库编号', '原始单号', '订单状态', '发货状态', '订单类型', '发货条件', '冻结原因', '退款状态', '分销类别', '分销商信息', '下单时间', '付款时间', '客户名称', '客户编码', '买家付款账号', '客户网名', '收件人', '收件人省份', '收件人城市', '收件人地区', '地址', '手机', '电话', '邮编', '省市县', '区域', '大头笔', '派送时间', '异常订单', '物流公司ID', '物流公司名称', '物流公司编号', '物流方式', '物流单号', '店铺id', '仓库id', '审核步骤', '未合并标记', '延迟处理', '客户类型', '买家id', '收件人国家', '预配送时间', '是否京配', '便签条数', '买家留言条数', '客服备注条数', '客服备注变化', '优惠变化金额', '使用的预存款', '分期付款金额', '其它成本', '体积', '销售积分', '标记id', '不可合并拆分', '赠品标记', '拆分订单，原单ID', '物流单模板ID', '发货单模板id', '驳回原因', '取消原因', '催未付款订单消息发送标记', '包装id', '订单标记位', '保留', '大件类型', '买家留言', '客服备注', '标旗', '打印备注', '货品种类数', '货品总数', '货品总额', '邮资', '其它费用', '折扣', '应收金额', '款到发货金额', 'COD金额', '买家COD费用', '货品预估成本', '邮资成本', '已付金额', '预估重量', '预估毛利', '税额', '税率', '佣金', '发票类型', '发票抬头', '发票内容', '业务员', '审核人', '业务员姓名', '审核人', '财审人', '签出人', '出库单号', '标记名称', '订单来源', '货品商家编码', '原始货品数量', '原始货品种类数', '币种', '线上包裹拆分数', '发票ID', '版本号', '最后修改时间', '订单生成时间', '证件类别', '证件号码', '财审人名称', '签出人名称', '冻结原因名称', '分销原始单号', 'tags']
            //if(headers.Length<125)
            //{
            //    return new Result(false, "请选择所有的列");
            //}

            ind = headerEnd+1;

            List<tradeData> list = new List<tradeData>();

            while (true)
            {
                var next = data.IndexOf('[', ind);
                if (next < 0)
                {
                    break;
                }

                var nextEnd = data.IndexOf(']', next);
                if (nextEnd < 0)
                    break;


                var valuesText = data.Substring(next + 1, nextEnd - next - 1);
                var arr = valuesText
                      .Split(',')
                    .Select(a => a.Trim().Trim('\''))
                    .ToArray();


                var item = new tradeData()
                {
                    trade_id = arr[0].IsNull(0),
                    trade_no = arr[1],
                    platform_id = arr[2].IsNull<short>(),
                    shop_platform_id = arr[3].IsNull<short>(),
                    shop_no = arr[4],
                    shop_name = arr[5],
                    shop_remark = arr[6],
                    warehouse_type = arr[7].IsNull<short>(),
                    warehouse_no = arr[8],
                    src_tids = arr[9],
                    trade_status = arr[10].IsNull<short>(),
                    consign_status = arr[11].IsNull(0),
                    trade_type = arr[12].IsNull<short>(),
                    delivery_term = arr[13].IsNull<short>(),
                    freeze_reason = arr[14].IsNull<short>(),
                    refund_status = arr[15].IsNull<short>(),
                    fenxiao_type = arr[16].IsNull<short>(),
                    fenxiao_nick = arr[17],
                    trade_time = arr[18].IsNullDate(DateTime.MinValue) ?? DateTime.MinValue,
                    pay_time = arr[19].IsNull(DateTime.MinValue),
                    customer_name = arr[20],
                    customer_no = arr[21],
                    pay_account = arr[22],
                    buyer_nick = arr[23],
                    receiver_name = arr[24],
                    receiver_province = arr[25].IsNull(0),
                    receiver_city = arr[26].IsNull(0),
                    receiver_district = arr[27].IsNull(0),
                    receiver_address = arr[28],
                    receiver_mobile = arr[29],
                    receiver_telno = arr[30],
                    receiver_zip = arr[31],
                    receiver_area = arr[32],
                    receiver_ring = arr[33],
                    receiver_dtb = arr[34],
                    to_deliver_time = arr[35],
                    bad_reason = arr[36].IsNull(0),
                    logistics_id = arr[37].IsNull<short>(),
                    logistics_name = arr[38],
                    logistics_code = arr[39],
                    logistics_type = arr[40].IsNull<short>(),
                    logistics_no = arr[41],
                    shop_id = arr[42].IsNull<short>(),
                    warehouse_id = arr[43].IsNull<short>(),
                    check_step = arr[44].IsNull<short>(),
                    unmerge_mask = arr[45].IsNull<short>(),
                    delay_to_time = arr[46],
                    customer_type = arr[47].IsNull<short>(),
                    customer_id = arr[48],
                    receiver_country = arr[49].IsNull<short>(),
                    pre_charge_time = arr[50],
                    is_prev_notify = arr[51].IsNull<short>(),
                    note_count = arr[52].IsNull<short>(),
                    buyer_message_count = arr[53].IsNull<short>(),
                    cs_remark_count = arr[54].IsNull<short>(),
                    cs_remark_change_count = arr[55].IsNull<short>(),
                    discount_change = arr[56].IsNull(0m),
                    trade_prepay = arr[57].IsNull(0m),
                    pi_amount = arr[58].IsNull(0m),
                    other_cost = arr[59].IsNull(0m),
                    volume = arr[60].IsNull(0m),
                    sales_score = arr[61],
                    flag_id = arr[62].IsNull<short>(),
                    is_sealed = arr[63].IsNull<short>(),
                    gift_mask = arr[64].IsNull<short>(),
                    split_from_trade_id = arr[65],
                    logistics_template_id = arr[66],
                    sendbill_template_id = arr[67],
                    revert_reason = arr[68].IsNull<short>(),
                    cancel_reason = arr[69].IsNull<short>(),
                    is_unpayment_sms = arr[70].IsNull<short>(),
                    package_id = arr[71],
                    trade_mask = arr[72],
                    reserve = arr[73],
                    large_type = arr[74].IsNull<short>(),
                    buyer_message = arr[75],
                    cs_remark = arr[76],
                    remark_flag = arr[77].IsNull<short>(),
                    print_remark = arr[78],
                    goods_type_count = arr[79].IsNull<short>(),
                    goods_count = arr[80].IsNull(0m),
                    goods_amount = arr[81].IsNull(0m),
                    post_amount = arr[82].IsNull(0m),
                    other_amount = arr[83].IsNull(0m),
                    discount = arr[84].IsNull(0m),
                    receivable = arr[85].IsNull(0m),
                    dap_amount = arr[86].IsNull(0m),
                    cod_amount = arr[87].IsNull(0m),
                    ext_cod_fee = arr[88].IsNull(0m),
                    goods_cost = arr[89].IsNull(0m),
                    post_cost = arr[90].IsNull(0m),
                    paid = arr[91].IsNull(0m),
                    weight = arr[92].IsNull(0m),
                    profit = arr[93].IsNull(0m),
                    tax = arr[94].IsNull(0m),
                    tax_rate = arr[95].IsNull(0m),
                    commission = arr[96].IsNull(0m),
                    invoice_type = arr[97].IsNull<short>(),
                    invoice_title = arr[98],
                    invoice_content = arr[99],
                    salesman_id = arr[100].IsNull(0),
                    checker_id = arr[101].IsNull(0),
                    fullname = arr[102],
                    checker_name = arr[103],
                    fchecker_id = arr[104].IsNull(0),
                    checkouter_id = arr[105].IsNull(0),
                    stockout_no = arr[106],
                    flag_name = arr[107],
                    trade_from = arr[108].IsNull<short>(),
                    single_spec_no = arr[109],
                    raw_goods_count = arr[110].IsNull(0m),
                    raw_goods_type_count = arr[111].IsNull(0),
                    currency = arr[112].IsNull(),
                    split_package_num = arr[113].IsNull(0),
                    invoice_id = arr[114].IsNull(0),
                    version_id = arr[115].IsNull<short>(),
                    modified = arr[116].IsNull(DateTime.MinValue),
                    created = arr[117].IsNull(DateTime.MinValue),
                    id_card_type = arr[118].IsNull<short>(),
                    id_card = arr[119],
                    fchecker_name = arr[120],
                    checkouter_name = arr[121],
                    freeze_reason_info = arr[122],
                    fenxiao_tid = arr[123],
                };


                list.Add(item);
                ind = nextEnd + 1;
            }


            //保存到数据库
            await _sql.Insert<tradeData>(list).ExecuteAffrowsAsync();
                

            return new Result(true);
        }
    }
}
